package com.hspedu.newData.dmsxl.树.构造二叉树;

import com.hspedu.newData.dmsxl.ds.TreeNode;
import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-13 
 * @Version: 1.0
 */

public class LC654最大二叉树 {

    public TreeNode constructMaximumBinaryTree(int[] nums) {

        TreeNode treeNode = build(nums, 0, nums.length);
        return treeNode;
    }

    public int maxNums(int[] nums,int begin,int end){
        int max = nums[begin];
        int res = begin;
        for (int i = begin; i < end; i++) {
            if (nums[i] > max){
                max = nums[i];
                res = i;
            }
        }
        return res;
    }

    public TreeNode build(int[] nums,int begin,int end){
        if (begin>=end){
            return null;
        }

        int i = maxNums(nums, begin, end);

        TreeNode root = new TreeNode(nums[i]);

        root.left = build(nums,begin,i);
        root.right = build(nums,i+1,end);

        return root;
    }

    @Test
    public void test() {
        // 测试代码

        int i = maxNums(new int[]{3, 2, 1, 6, 0, 5}, 0, 6);

    }
}
